package pl.drypisy;

import java.util.ArrayList;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ProgressBar;

import com.discalis.android.maps.BalloonLayout;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/** 
 * @author Marcin Piłat
 * @version 1.0
 */
public class CheckpointItemizedOverlay extends ItemizedOverlay<OverlayItem> {
	
	private ArrayList<OverlayItem> myOverlays = new ArrayList<OverlayItem>();
	
	private final static String TAG = "Discalis BIO";

    protected final MapView mapView;
    protected BalloonLayout balloonView;
    protected View clickRegion;
    protected final int balloonViewOffset;
	
    /**
     * Public constructor
     * @param defaultMarker Drawable which will be printed on map with this overlay
     * @param mv Handler to MapView from PlayMapActivity
     */
	public CheckpointItemizedOverlay(Drawable defaultMarker,MapView mv) {
		super(boundCenterBottom(defaultMarker));
        mapView = mv;
        balloonViewOffset = defaultMarker.getIntrinsicHeight();
        populate();
	}
	
	//On baloon tap
	protected boolean onBalloonTap(int index) {
        hideBalloon();
        return true;
	}
	
	/**
	 * Add Item which will be printed on map.
	 * @param oI Item which will be added to ItemizedOverlay
	 */
	public void addOverlay(OverlayItem oI){
		myOverlays.add(oI);
		populate();
	}
	
	/**
	 * Clean all overlay.
	 */
	public void clearOverlay(){
		myOverlays.clear();
		populate();
	}
	
	@Override
	protected OverlayItem createItem(int i) {
		return myOverlays.get(i);
	}

	/**
	 * 
	 * @see com.google.android.maps.ItemizedOverlay#size()
	 * @return Amount of item.
	 */
	@Override
	public int size() {
		return myOverlays.size();
	}
	
	/*Code onTap function written by isaac.salgueiro@discalis.com (based on Jeff Gilfelt's
	*         BalloonItemizedOverlay)
	*/
	@Override
    protected final boolean onTap(int index) {

            boolean isRecycled = true;
            final int thisIndex = index;
            final GeoPoint point = createItem(index).getPoint();

            if (balloonView == null) {
                    Log.d(TAG, "New balloonView");
                    balloonView = new BalloonLayout(mapView.getContext(), balloonViewOffset);
                    clickRegion = (View) balloonView.findViewById(R.id.balloon_inner_layout);
                    isRecycled = false;
            }

            balloonView.setVisibility(View.GONE);

            if (mapView.getOverlays().size() > 1) {
                    Log.d(TAG, "Hidding balloons from other overlays");
                    final List<Overlay> overlays = mapView.getOverlays();
                    for (Overlay overlay : overlays) {
                    	//Change by Marcin Piłat 
                            if ((overlay != this) && (overlay instanceof CheckpointItemizedOverlay)) {
                                    ((CheckpointItemizedOverlay) overlay).hideBalloon();
                            }
                    }
            }

            balloonView.setText(createItem(index).getTitle(),"");

            MapView.LayoutParams params = new MapView.LayoutParams(LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT, point, MapView.LayoutParams.BOTTOM_CENTER);
            params.mode = MapView.LayoutParams.MODE_MAP;

            clickRegion.setOnTouchListener(new OnTouchListener() {
                    public boolean onTouch(View arg0, MotionEvent arg1) {
                            if (arg1.getAction() == MotionEvent.ACTION_UP) {
                                    return onBalloonTap(thisIndex);
                            }
                            return true;
                    }
            });

            balloonView.setVisibility(View.VISIBLE);

            if (isRecycled) {
                    balloonView.setLayoutParams(params);
            } else {
                    mapView.addView(balloonView, params);
            }

            // TODO config parameter to determine wether we center the map or not
            mapView.getController().animateTo(point);

            return true;
    }
	
	/*End of written by isaac.salgueiro@discalis.com (based on Jeff Gilfelt's
	*         BalloonItemizedOverlay)
	*/
	
	
	private boolean hideBalloon() {
        if ((balloonView != null) && (balloonView.getVisibility() != View.GONE)) {
                Log.d(TAG, "Hidding balloon");
                balloonView.setVisibility(View.GONE);
                return true;
        }
        return false;
}

}